package com.pan.frame;

import com.pan.entity.Chat;
import com.pan.entity.transmit.Packet;
import com.pan.enumn.MessageEnum;
import com.pan.net.Communication;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.text.SimpleDateFormat;

public class ChatFrame {

    private JFrame frame;
    private JTextArea chatTxtArea;
    private JTextArea msgTxtArea;
    private Font font;

    private JButton sendButton;

    private Communication communication;



    private String name;
    private static final SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");


    public ChatFrame(Communication communication,
                     String name) {
        this.communication = communication;
        this.name = name;
    }

    public void init(){
        frame = new JFrame("Pan.Qi.An");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(600, 600);
        frame.setLayout(null);
        frame.setResizable(false);


        chatTxtArea = new JTextArea();
        chatTxtArea.setEditable(false);
        chatTxtArea.setLineWrap(true);
        JScrollPane chatScrollPane = new JScrollPane(chatTxtArea);
        chatScrollPane.setBounds(4,0,580,400);
        frame.add(chatScrollPane);


        msgTxtArea = new JTextArea();
        msgTxtArea.setText("");
        msgTxtArea.setLineWrap(true);
        JScrollPane msgScrollPane = new JScrollPane(msgTxtArea);
        msgScrollPane.setBounds(4,440,580,120);
        frame.add(msgScrollPane);


        font = new Font("SimSun", Font.PLAIN, 18);
        msgTxtArea.setFont(font);
        chatTxtArea.setFont(font);


        sendButton = new JButton("发送");
        sendButton.setBounds(500,405,80,30);
        sendButton.addActionListener((e)->{
            sendMsg();
        });
        sendButton.setMnemonic(KeyEvent.VK_ENTER);
        frame.add(sendButton);

        monitor();
        frame.setVisible(true);
    }

    public void sendMsg() {
        String msg = msgTxtArea.getText();
        if (msg== null||msg.length()==0){
            return;
        }
        msgTxtArea.setText("");


        Chat chat = new Chat(
                name,
                System.currentTimeMillis(),
                msg
        );
        Packet<Chat> packet = new Packet<>(MessageEnum.CHAT, chat);
        communication.send(packet);
        showChat(chat);
    }

    private Thread monitorThread;
    public void monitor(){
        monitorThread = new Thread(()->{
            while (true){
                Packet receive = communication.receive();
                packetSort(receive);
            }
        });
        monitorThread.start();
    }

    public void close(){
        monitorThread.interrupt();
    }

    public boolean equalsPacket(Packet packet,MessageEnum messageEnum){
        if (packet.getCode() == messageEnum.getCode())
            return true;
        else
            return false;
    }
    public void packetSort(Packet packet){
        if (equalsPacket(packet,MessageEnum.CHAT)){
            Chat data = (Chat) packet.getData();
            showChat(data);
        } else if (equalsPacket(packet,MessageEnum.CLOSE)) {

        }

    }

    public void showChat(Chat cv){
        String name = cv.getName();
        String content = cv.getContent();
        String date =  sdf.format(cv.getTime());
        String msg = name+" （"+date + "） :\n" + content + "\n\n";
        chatTxtArea.append(msg);
    }


    public static void main(String[] args) throws IOException {
//        Server server = new Server();
//        new ChatFrame().init();
    }




}
